package server;


import java.util.Date;

/**
 * Serves for stucture, encapsulating ResponseHeader fields.
 * @author Peewhy
 */
public class ResponseHeader {
    public final static int DEFAULT_STATUS = 400;
    public final static String DEFAULT_CTYPE = "text/html";
    public final static String DEFAULT_CHARS = "utf-8";
    public final static String DEFAULT_SERVER = "Web Server/1.0.0 (Unix)";
    
    public final static int CONNECTION_CLOSE = 0;
    public final static int CONNECTION_KEEPALIVE = 1;    
    public static final String CRLF = "\r\n";
    

    private Date date;
    
    private String charset;
    private String server;
    private String contentType;
    private String contentEncoding;    
    private String transferEncoding;
    private String status;
    
    private int contentLength;
    private Date lastModified;
    private int connection;
    
    
    public void setStatus(int code) {
        status = Server.VERSION + " " + code + " " + ErrorCode.getString(code);
    }
    
    public void setStatus(int code, String arg) {
        status = Server.VERSION + " " + code + " " + ErrorCode.getString(code) + ": " + arg;
    }
    
    public String getStatus(){
        return status;
    }

    public String getfContentLength(){
        return new Integer(contentLength).toString();
    }
    
    public String getfContentType(){
        return contentType;
    }
    
    public String getfConnection () {
        switch (connection) {
            case CONNECTION_CLOSE:   return "close";
            case CONNECTION_KEEPALIVE: return "Keep-alive";
        }
        return  "close";
    }
    
    public String getfDate(Date d) {
        return HttpDate.getFormattedDate(d);
    }
    
    public String getfServer() {
        return server;
    }
    
    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public int getConnection() {
        return connection;
    }

    public void setConnection(int connection) {
        this.connection = connection;
    }

    public String getContentEncoding() {
        return contentEncoding;
    }

    public void setContentEncoding(String contentEncoding) {
        this.contentEncoding = contentEncoding;
    }

    public int getContentLength() {
        return contentLength;
    }

    public void setContentLength(int contentLength) {
        this.contentLength = contentLength;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getLastModified() {
        return lastModified;
    }

    public void setLastModified(Date lastModified) {
        this.lastModified = lastModified;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }


    public String getTransferEncoding() {
        return transferEncoding;
    }

    public void setTransferEncoding(String transferEncoding) {
        this.transferEncoding = transferEncoding;
    }
    
    /**
     * Creates header with default constant values
     */
    public ResponseHeader() {
        this.setStatus(200);
        date = new Date();
        contentType = DEFAULT_CTYPE;
        charset = DEFAULT_CHARS;
        contentLength = 0;
        server = DEFAULT_SERVER;
    }
    
    /* Helper function for printing argument seperated by " " from value,
     * and in addition ending string with CRLF */     
    private String headerLine(String arg, String val) {
        return arg + ": " + val + CRLF;
    }
    
    /**
     * Returns headers formated, as String, in HTTP response format
     * @return
     */
    public String getHeaders() {
        String headers = status + CRLF;
        headers += headerLine("Connection", getfConnection());
        headers += headerLine(("Date"), getfDate(date));
        headers += headerLine("Server", getfServer());
        headers += headerLine("Content-length", getfContentLength());
        headers += headerLine("Content-type", getfContentType());
        return headers;
    }
}
